package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import static com.hankcs.hanlp.corpus.tag.Nature.wm;

/**
 * @author 黄晏凯
 */
@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    @Autowired
    private WmChannelMapper wmChannelMapper;

    @Autowired
    private WmNewsMapper wmNewsMapper;

    /**
     * 查询所有频道
     *
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult findAll() {
        return ResponseResult.okResult(list());
    }

    /**
     * 列表
     *
     * @param dto dto
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult list(ChannelDto dto) {
        if (null == dto) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        //整合
        IPage<WmChannel> page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmChannel> lqw = new LambdaQueryWrapper<>();
        //名字模糊查询
        if (StringUtils.isNotBlank(dto.getName())) {
            lqw.like(WmChannel::getName, dto.getName());
        }
        //创建时间倒序
        lqw.orderByDesc(WmChannel::getCreatedTime);
        //查询
        page = page(page, lqw);
        //返回
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    /**
     * 新增频道
     *
     * @param wmChannel wm通道
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult saveChannel(WmChannel wmChannel) {
        if (null == wmChannel) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (null == wmChannel.getName() || null == wmChannel.getOrd()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        WmChannel selectOne = wmChannelMapper.selectOne(Wrappers.<WmChannel>lambdaQuery()
                .eq(WmChannel::getName, wmChannel.getName()));
        if (null != selectOne) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        wmChannel.setIsDefault(true);
        wmChannel.setCreatedTime(new Date());
        save(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改频道
     *
     * @param wmChannel wm通道
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult updateChannel(WmChannel wmChannel) {
        if (null == wmChannel ||
                null == wmChannel.getId() ||
                null == wmChannel.getName()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 频道与新闻挂钩
        List<WmNews> news = wmNewsMapper.selectList(Wrappers.<WmNews>lambdaQuery()
                .eq(WmNews::getChannelId, wmChannel.getId()));
        if (news.size() != 0 && !wmChannel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_CITED);
        }

        if (null != wmChannel.getDescription()) {
            WmChannel one = getOne(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getName, wmChannel.getName()));
            if (null != one) {
                if (!one.getId().equals(wmChannel.getId())) {
                    return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
                }
            }

        }
        updateById(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除频道
     *
     * @param id id
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult deleteChannel(Integer id) {
        if (null == id) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        WmChannel channel = getById(id);
        if (channel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH, "启用中，无法删除");
        }

        int i = wmChannelMapper.deleteById(id);
        if (1 != i) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "删除失败");
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}